package com.ler.yrmall.product.controller;


import com.ler.yrmall.basic.Result;
import com.ler.yrmall.product.entity.SpuEntity;
import com.ler.yrmall.product.form.SpuForm;
import com.ler.yrmall.product.service.SpuService;
import com.ler.yrmall.product.vo.SpuVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * spu
 * @author Leron
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Resource
    private SpuService spuInfoService;

    @RequestMapping("/sale/{spuId}")
    public Result<?> sale(@PathVariable Long spuId){
        spuInfoService.spuSell(spuId);
        return Result.ok();
    }

    @RequestMapping("/soldOut/{spuId}")
    public Result<?> soldOut(@PathVariable Long spuId){
        spuInfoService.soldOut(spuId);
        return Result.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/find")
    public Result<?> find(SpuForm request) {
        return Result.ok(spuInfoService.find(request));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result<?> list() {
        return Result.ok(spuInfoService.list());
    }


    /**
     * 信息
     */
    @RequestMapping("/getVoById/{id}")
    public Result<?> getById(@PathVariable("id") Long id) {
        SpuVO spuInfoVO = spuInfoService.getSpuVoById(id);
        return Result.ok(spuInfoVO);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<?> save(@RequestBody SpuForm request) {
        spuInfoService.saveSpu(request);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody SpuForm request) {
        spuInfoService.updateSpu(request);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        spuInfoService.deleteSpu(id);
        return Result.ok();
    }

    @GetMapping("/infoBySkuId")
    public Result<?> infoBySkuId(@RequestParam("skuId") Long skuId) {
        SpuEntity spuInfo = spuInfoService.getSpuInfoBySkuId(skuId);
        return Result.ok(spuInfo);
    }

    @GetMapping("/getSaleAttrBySpuId/{spuId}")
    public Result<?> getSaleAttrBySpuId(@PathVariable("spuId") Long spuId) {
        return Result.ok(spuInfoService.getSaleAttrBySpuId(spuId));
    }

    // ------- webapp  ------

    @GetMapping("/getById/{spuId}")
    public Result<?> getById(@PathVariable long spuId) {
        return Result.ok(spuInfoService.spuView(spuId));
    }

}
